<%@ page pageEncoding="utf-8" %>
<%@include file="/commons/taglibs.jsp" %>
<!DOCTYPE html>
<html>
<fmt:bundle basename="res">
<head>
    <meta charset="utf-8">
    <title><fmt:message key='ttbl' /></title>
    <jsp:include page="/WEB-INF/commons/commons-header.jsp" flush="true"/>
    <style>
        html {
            background: #fff;
        }
    </style>
</head>

<body>
<div class="layui-container">
    <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">

        <form:form commandName="seckill" method="${_method }" class="layui-form" id="form">
            <input type="hidden" name="_method" value="${_method }"/>
            <form:hidden path="id"/>

            <div class="layui-form-item">
                <label class="layui-form-label"><b style="color:red">*</b> <fmt:message key='mscc' /></label>
                <div class="layui-input-block" style="width:200px;">
                    <select id="seckillSession" lay-filter="seckillSession" name="seckillSession.id">
                        <option value="0"><fmt:message key='qxz' /></option>
                        <c:forEach items="${seckillSessions}" var="session">
                            <option value="${session.id}" <c:if test="${session.id == seckill.seckillSession.id}">selected="selected"</c:if>>${session.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"> <fmt:message key='msrq' /></label>
                <div class="layui-form-mid layui-word-aux" id="seckillTime">

                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label"> <fmt:message key='mssj' /></label>
                <div class="layui-form-mid layui-word-aux" id="seckillRoundTime">

                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label"> <fmt:message key='bmjzsj' /></label>
                <div class="layui-form-mid layui-word-aux" id="applyDeadline">

                </div>
            </div>

            <div class="layui-form-item feijin-zenpin-area">
                <label class="layui-form-label"><b style="color:red">*</b> <fmt:message key='mssp' /></label>
                <div class="layui-input-block">
                    <table class="layui-table" lay-size="sm" id="feijin-sku-table">
                        <thead>
                        <tr>
                            <th><fmt:message key='spbh' /></th>
                            <th><fmt:message key='spmc' /></th>
                            <th><fmt:message key='scj' /></th>
                            <th><fmt:message key='msjzs' /></th>
                            <th><fmt:message key='gg' /></th>
                            <th><fmt:message key='kc' /></th>
                            <th><fmt:message key='ysjg' /></th>
                            <th><fmt:message key='hdkc' /></th>
                            <th><fmt:message key='hdjg' /></th>
                            <th><fmt:message key='xgsl' /></th>
                        </tr>
                        </thead>
                        <tbody id="goodsArea">

                        <c:if test="${_method eq 'PUT'}">
                            <c:forEach items="${seckillGoodses}" var="item">
                                <tr data-id='${item.goods.id}' class='feijin-goods-ids'>
                                    <td rowspan='${fn:length(item.goods.skus) + 1}'>${item.goods.goodsNo}</td>
                                    <td rowspan='${fn:length(item.goods.skus) + 1}'>${item.goods.name}</td>
                                    <td rowspan='${fn:length(item.goods.skus) + 1}'>${item.goods.defaultPrice }</td>
                                    <td rowspan='${fn:length(item.goods.skus) + 1}'><input value="${item.goods.seckillPrice}" data-id='${item.goods.id}' class='layui-input goodsSeckillPrice' type='text'/></td>
                                </tr>

                                <c:forEach items="${item.goods.skus}" var="sku">
                                    <tr class='skus' data-id='${sku.id}'>
                                        <td>${sku.skuNames}</td>
                                        <td class='feijin-old-store'>${sku.store}</td>
                                        <td class='feijin-old-price'>${sku.price }</td>
                                        <td><input type='text' value="${sku.seckillStore}" class='layui-input' id='seckillStore${sku.id}'/></td>
                                        <td><input type='text' value="${sku.seckillPrice}" class='layui-input' id='seckillPrice${sku.id}'/></td>
                                        <td><input type='text' value="${sku.seckillLimit}" class='layui-input' id='seckillLimit${sku.id}'/></td>
                                    </tr>
                                </c:forEach>

                            </c:forEach>
                        </c:if>
                        <tr id="select-goods-tr">
                            <td colspan="10">
                                <center><a href="javascript:void(0);" id="chooseGiftGoods" style="color:blue;"><fmt:message key='xzsp' /></a></center>
                            </td>
                        </tr>
                        </tbody>

                    </table>
                </div>
            </div>

            <div class="layui-input-block">
                <div style="display: none">
                    <input type="button" value="<fmt:message key='bc' />" class="layui-btn" id="submit" lay-filter="submit" lay-submit>
                </div>
            </div>

            <input type="hidden" name="goodsString" id="goodsString"/>
            <input type="hidden" name="skuString" id="skuString"/>

        </form:form>
    </div>
</div>
<script>
    $(function () {

        $.ajax({
            url: "${web_domain}/merchant/seckill/querySession?id=${seckill.seckillSession.id}",
            type: "get",
            dataType: "json",
            success: function (result) {
                if (result.status) {
                    $("#seckillTime").html(result.seckillSession.seckillTime);
                    $("#seckillRoundTime").html(result.seckillSession.seckillBeginTime + " ~ " + result.seckillSession.seckillEndTime);
                    $("#applyDeadline").html(result.seckillSession.applyDeadline);
                }
            }
        });

        var wIds = [];

        //选择赠品
        $("#chooseGiftGoods").click(function () {
            layer.open({
                type: 2
                , title: '<fmt:message key='xzsysp' />'
                , content: '${web_domain}/merchant/seckill/chooseGoods?ids=' + wIds.join("-")
                , maxmin: true
                , area: ['80%', '90%']
                , btn: ['<fmt:message key='qd' />', '<fmt:message key='qx' />']
                , yes: function (index, layero) {

                    var ids = [];

                    layero.find('iframe').contents().find(".feijin-check:checkbox:checked").each(function (index, item) {
                        var id = $(item).val();
                        if (wIds.indexOf(id) === -1) {
                            wIds.push(id);
                            ids.push(id);
                        }
                    });

                    if (ids.length === 0) {
                        return;
                    }

                    $.ajax({
                        url: "${web_domain}/merchant/seckill/queryGoodses?ids=" + ids.join("-"),
                        type: "get",
                        dataType: "json",
                        success: function (result) {
                            if (result.status) {

                                var html = "";

                                $(result.goodses).each(function (index, item) {
                                    html += "<tr data-id='" + item.id + "' class='feijin-goods-ids' >";
                                    html += "<td rowspan='" + (item.skus.length + 1) + "'>" + item.goodsNo + "</td>";
                                    html += "<td rowspan='" + (item.skus.length + 1) + "'>" + item.name + "</td>";
                                    html += "<td rowspan='" + (item.skus.length + 1) + "'>" + item.defaultPrice + "</td>";
                                    html += "<td rowspan='" + (item.skus.length + 1) + "'><input data-id='" + item.id + "' class='layui-input goodsSeckillPrice' type='text' /></td>";
                                    html += "</tr>";

                                    $(item.skus).each(function (skuIndex, sku) {
                                        html += "<tr class='skus' data-id='" + sku.id + "'>"
                                        html += "<td >" + sku.skuNames + "</td>";
                                        html += "<td class='feijin-old-store'>" + sku.store + "</td>";
                                        html += "<td class='feijin-old-price'>" + sku.price + "</td>";
                                        html += "<td ><input type='text' class='layui-input' id='seckillStore" + sku.id + "'/></td>";
                                        html += "<td ><input type='text' class='layui-input' id='seckillPrice" + sku.id + "'/></td>";
                                        html += "<td ><input type='text' class='layui-input' id='seckillLimit" + sku.id + "'/></td>";
                                        html += "</tr>"
                                    });
                                });
                                $("#select-goods-tr").before(html);
                            }
                        }
                    });

                    layer.closeAll();

                }
            });
        });


        layui.use(['form', 'laydate'], function () {

            var form = layui.form;
            var laydate = layui.laydate;

            form.on('select(seckillSession)', function (data) {

                if (data.elem != data.value) {
                    var id = data.value;
                    if (id == 0) {
                        $("#seckillTime").html("");
                        $("#seckillRoundTime").html("");
                        $("#applyDeadline").html("");
                        return false;
                    }

                    $.ajax({
                        url: "${web_domain}/merchant/seckill/querySession?id=" + id,
                        type: "get",
                        dataType: "json",
                        success: function (result) {
                            if (result.status) {
                                $("#seckillTime").html(result.seckillSession.seckillTime);
                                $("#seckillRoundTime").html(result.seckillSession.seckillBeginTime + " ~ " + result.seckillSession.seckillEndTime);
                                $("#applyDeadline").html(result.seckillSession.applyDeadline);
                            }
                        }
                    });


                }
            });

            form.on('submit(submit)', function (data) {

                var goodsSeckillPrice = [];
                $(".goodsSeckillPrice").each(function (index, item) {
                    var id = $(item).attr("data-id");
                    goodsSeckillPrice.push(id + ":" + $(item).val());
                });

                var skus = [];
                var content = '';

                $(".skus").each(function (index, item) {
                    var id = $(item).attr("data-id");
                    var seckillStore = $("#seckillStore" + id).val();
                    var seckillPrice = $("#seckillPrice" + id).val();
                    var seckillLimit = $("#seckillLimit" + id).val();

                    if (!seckillStore || !seckillPrice || !seckillLimit) {
                        content = "<fmt:message key='hdcsbyxwk' />";
                        return false;
                    }

                    var oldStore = $(".feijin-old-store:eq(" + index + ")").html();
                    var oldPrice = $(".feijin-old-price:eq(" + index + ")").html();

                    if (parseFloat(seckillPrice) > parseFloat(oldPrice)) {
                        content = "<fmt:message key='hdjbyxdyyxsj' />";
                        return false;
                    }

                    if (parseInt(seckillStore) > parseInt(oldStore)) {
                        content = "<fmt:message key='hdkcbyxdyykc' />";
                        return false;
                    }

                    skus.push(id + ":" + seckillStore + "-" + seckillPrice + "-" + seckillLimit);

                });

                if (content != '') {
                    layer.msg(content);
                    content = '';
                    return false;
                }

                $("#goodsString").attr("value", goodsSeckillPrice.join(";"));
                $("#skuString").attr("value", skus.join(";"));

                $("#form").ajaxSubmit({
                    success: function (result) {
                        if (result.status) {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.$("#search").click();
                        } else {
                            layui.use('layer', function () {
                                layer.msg(result.message);
                            });
                        }
                    }
                });
            });

        });

    });
</script>
</body>
</fmt:bundle>
</html>